{extend name="mainadmin@layouts/base" /}
{block name="head" }
<link rel="stylesheet" href="__STATIC__/js/jquery/jquery_ui/jquery-ui.css">
<script src="__STATIC__/js/jquery/jquery_ui/jquery-ui.js"></script>

{/block}
{block name="main" }
<header class="header  b-b clearfix">
    <div class="page-breadcrumbs">
        <ul class="breadcrumb">
            <li>
                <i class="fa fa-ellipsis-v"></i>
                <strong>{$row['id']<1?'添加代理商品':'编缉代理商品'}</strong>
            </li>
        </ul>
        <a class="text-muted pull-right m-r-tm m-t-md pointer" data-toggle="back" title="返回"><i class="fa fa-reply"></i></a>
    </div>
</header>
<form class="form-horizontal form-validate form_vbox" method="post" action="{:url('info')}">
    <section class="vbox">
        <section class="scrollable wrapper w-f">
            <section class="panel panel-default">
                <header class="panel-heading bg-light">
                    商品信息
                </header>
                <div class="form-group m-t-md">
                    <label class="col-sm-2 control-label">商品名称：</label>
                    <div class="col-sm-7 m-t-mc">
                        {$goodsInfo.goods_name}
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">商品分类：</label>
                    <div class="col-sm-3 m-t-mc">
                        {$className}
                    </div>
                    <label class="col-sm-2 control-label">商品规格：</label>
                    <div class="col-sm-3 m-t-mc">
                        {$goodsInfo['is_spec'] == 1?'多规格':'单规格'}
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">上架时间：</label>
                    <div class="col-sm-10" >
                        <label class="radio-inline">
                            <input type="radio" name="isputaway" value="1"  {$row.isputaway|tplckval='=1','checked',true}  />立即上架
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="isputaway"   value="0" {$row.isputaway|tplckval='=0','checked'} >暂不上架，放入仓库中
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="isputaway" value="2" {$row.isputaway|tplckval='=2','checked'}/>设定商品上下架时间
                        </label>
                        <div class="clearfix m-t-md"></div>
                        <div class="col-sm-1" style="width:70px;padding:0; margin:0;">
                            <label style="padding:0;margin:0;">上架时间</label>
                        </div>
                        <div class="col-sm-3"  style="width:180px;padding:0; margin:0;">
                            <div class="input-group"> <input type="text" class="input-max" name="added_time" readonly="readonly" value="{$row.added_time|dateTpl='Y-m-d H:i',true}" data-before="#solddate"   data-toggle="datetimepicker" /><span class="input-group-addon"><i class="fa fa-calendar"></i></span></div>
                        </div>
                        <div class="col-sm-1" style="width:70px;margin:0; margin-left:10px;">
                            <label style="padding:0;margin:0;">下架时间</label>
                        </div>
                        <div class="col-sm-3"  style="width:180px;padding:0; margin:0;">
                            <div class="input-group"> <input type="text" class="input-max" name="shelf_time" value="{$row.shelf_time|dateTpl='Y-m-d H:i',true}" data-after="#groundingdate" data-offsetday="0"  readonly="readonly" data-toggle="datetimepicker" /><span class="input-group-addon"><i class="fa fa-calendar"></i></span></div>
                        </div>

                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">默认计量单位：</label>
                    <div class="col-sm-4">
                        <select name="unit" id="select_unit" style="width:150px;" data-toggle="select2" data-notsearch="true" data-placeholder="请选择默认计量单位.">
                            <option value="">选择默认计量单位</option>
                            {volist name="goodsUnit" id="gu"}
                            <option value="{$gu.id}" {$row['unit']==$gu['id']?'selected':''}>{$gu.name}</option>
                            {/volist}
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">单位换算：</label>
                    <div class="col-sm-8">
                        <select id="select_convert_unit" style="width:150px;" data-notsearch="true"  data-toggle="select2">
                            <option value="">选择换算单位</option>
                            {volist name="goodsUnit" id="gu"}
                            <option value="{$gu.id}">{$gu.name}</option>
                            {/volist}
                        </select>
                        <a href="javascript:;" onclick="addConvertUnit()" class="btn btn-sm btn-default"><i class="fa fa-plus"></i>添加</a><span class="help-inline red">* 请按从小到大顺序添加</span>
                        <div id="convert_unit_box">
                            {volist name="$row['convert_unit']" id="val"}
                                <label><input type="hidden" name="convert_unit_id[]" value="{$key}" >1{$goodsUnit[$key]['name']} = <input class="input-ssmall m-t" data-rule-required="ture" data-rule-number="true" name="convert_unit_val[]" type="text" value="{$val}"> {$goodsUnit[$row['unit']]['name']} <a href="javascript:;" class="text-muted del_convert_unit"><i class="fa fa-times"></i></a></label>
                            {/volist}
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">价格规则：</label>
                    <div class="col-sm-10">
                        <label class="radio-inline">
                            <input name="price_type" class="price_type_radio" value="0" {$row.price_type|tplckval='<1' ,'checked'} type="radio" >统一出货价
                        </label>
                        {if condition="$goodsInfo['is_spec'] == 1"}
                        <label class="radio-inline">
                            <input name="price_type" class="price_type_radio"  value="1" {$row.price_type|tplckval='>0' ,'checked'}  type="radio" >独立出货价
                        </label>
                        {/if}
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">零售价：</label>
                    <div class="col-sm-10 m-t-mc">
                        {if condition="$goodsInfo['is_spec'] == 1"}
                            {$goodsInfo['min_price']} ~ {$goodsInfo['max_price']}
                        {else/}
                            {$goodsInfo['shop_price']}
                        {/if}
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">定价方式：</label>
                    <div class="col-sm-10 ">
                        <label class="radio-inline">
                            <input name="pricing_type" class="pricing_type" value="0" {$row.pricing_type|tplckval='=0' ,'checked',true}  type="radio" >固定价
                        </label>
                        <label class="radio-inline">
                            <input name="pricing_type" class="pricing_type" value="1" {$row.pricing_type|tplckval='=1' ,'checked'}  type="radio" >折扣价（根据零售价）
                        </label>
                    </div>
                </div>
                <header class="panel-heading bg-light">
                    渠道价格
                </header>
                <div class="panel-body" id="price_box">
                </div>
            </section>
        </section>
        <footer class="footer bg-white b-t p-t">
            <div class="form-group">
                <div class="col-sm-4 col-sm-offset-1">
                    <input name="id" type="hidden" value="{$row.id|intval}">
                    <input name="goods_id" type="hidden" value="{$goodsInfo['goods_id']}">
                    <button type="submit"  class="btn btn-primary js_save_submit" data-loading-text="保存中...">保存</button>
                    <button type="button" class="btn btn-default" data-toggle="back">取消</button>
                </div>
            </div>
        </footer>
    </section>
</form>
{/block}

{block name="footer" }
{literal}
<script type="text/html" id="price_type_more">
    <header>
        <ul class="nav nav-tabs">
            {{each proxyLevel as item index}}
            <li class="{{if index==0}}active{{/if}}" ><a href="#proxyLevel_{{item.proxy_id}}" data-proxy_id="{{item.proxy_id}}" data-toggle="tab">{{item.proxy_name}}</a></li>
            {{/each}}
        </ul>
    </header>
    <div class="tab-content">
        {{each proxyLevel as item index}}
        <div class="tab-pane {{if index==0}}active{{/if}}" id="proxyLevel_{{item.proxy_id}}">
            <table class="table table-hover table-bordered" style="width: 800px">
                <tr>
                    <td>规格</td>
                    <td>零售价</td>
                    <td >进货价
                        {{if item.proxy_id > 0}}
                        （{{item.supply_name}}）
                        {{/if}}
                    </td>
                    <td>层级定价</td>
                    <td>层级毛利</td>
                    <td>毛利率</td>
                </tr>
                {{each sub_goods as sg index}}
                <tr>
                    <td>{{sg.sku_name}}</td>
                    <td>{{sg.shop_price}}</td>
                    <td >
                        {{if item.proxy_id == 0}}
                            --
                        {{else}}
                            <span class="superior_price superior_price_{{item.pid}}_{{sg.sku_id}}"></span>
                        {{/if}}
                    </td>
                    <td>
                        <div class="input-group" style="width: 100px;">
                            <input type="text" class="input-mini sale_price" id="price_{{item.proxy_id}}_{{sg.sku_id}}"  data-supply_pid="{{item.supply_pid}}" data-pid="{{item.pid}}" data-proxy_id="{{item.proxy_id}}" data-sku_id="{{sg.sku_id}}" name="price[{{item.proxy_id}}][{{sg.sku_id}}]" data-rule-required="true" data-rule-ismoney="true" >
                            <span class="input-group-addon pricing_type_addon">
                               {{if pricing_type == 0}}元{{else}}%{{/if}}
                            </span>
                        </div>
                    </td>
                    <td>
                        {{if item.proxy_id == 0}}
                        --
                        {{else}}
                        <span id="gross_{{item.proxy_id}}_{{sg.sku_id}}"></span>
                        {{/if}}
                    </td>
                    <td>
                        {{if item.proxy_id == 0}}
                        --
                        {{else}}
                        <span id="gross_pre_{{item.proxy_id}}_{{sg.sku_id}}"></span>
                        {{/if}}
                    </td>
                </tr>
                {{/each}}
            </table>
        </div>
        {{/each}}
    </div>
</script>
<script type="text/html" id="price_type_one">
    <table class="table table-hover table-bordered" style="width: 800px">
        <tr>
            <td>层级名称</td>
            <td>进货价</td>
            <td>层级定价</td>
            <td>层级毛利</td>
            <td>毛利率</td>
        </tr>
        {{each proxyLevel as item index}}
            <tr>
                <td>{{item.proxy_name}}</td>
                <td >
                    {{if item.proxy_id == 0}}
                     --
                    {{else}}
                    <span class="superior_price superior_price_{{item.supply_pid}}_0"></span>
                    {{/if}}
                </td>
                <td>
                    <div class="input-group" style="width: 100px;">
                        <input type="text" class="input-mini sale_price" id="price_{{item.proxy_id}}_0"  data-supply_pid="{{item.supply_pid}}" data-pid="{{item.pid}}" data-proxy_id="{{item.proxy_id}}" data-sku_id="0" name="price[{{item.proxy_id}}]" data-rule-required="true" data-rule-ismoney="true" >
                        <span class="input-group-addon pricing_type_addon">
                             {{if pricing_type == 0}}元{{else}}%{{/if}}
                        </span>
                    </div>
                <td>
                {{if item.proxy_id == 0}}
                --
                {{else}}
                <span class="gross" id="gross_{{item.proxy_id}}_0"></span>
                {{/if}}
            </td>
                <td>
                    {{if item.proxy_id == 0}}
                    --
                    {{else}}
                    <span class="gross" id="gross_pre_{{item.proxy_id}}_0"></span>
                    {{/if}}
                </td>
            </tr>
        {{/each}}
    </tabel>
</script>
{/literal}
<script type="text/javascript">
    var is_spec = {$goodsInfo['is_spec']};
    var shop_price = {$goodsInfo['shop_price']};

    var prices = {$prices|json_encode|raw};
    var proxyLevel = {$proxyLevel|json_encode|raw};
    function setprice(){
        var price_type = $("input[name='price_type']:checked").val();
        var pricing_type = $("input[name='pricing_type']:checked").val();
        $.each(prices,function (i,v) {
            if (price_type != v.price_type){
                return true;
            }
            $('#price_'+v.proxy_id+'_'+v.sku_id).val(v.price);
        })
    }
    function setPriceBox(){
        var data = new Object();
        data.pricing_type = $('input[name="pricing_type"]:checked').val();
        data.proxyLevel = proxyLevel;
        var price_type = $("input[name='price_type']:checked").val();
        if (price_type == 1){ //单独价
            data.sub_goods = {$goodsInfo['sub_goods']|json_encode|raw};
            $('#price_box').html(template('price_type_more',data));
        }else{ //统一价
            $('#price_box').html(template('price_type_one',data));
        }
        setprice();
        changePrice();

    }

    setPriceBox();
    $('.price_type_radio').click(function (){
        setPriceBox();
    })

    $(document).on('input propertychange','.sale_price',function () {
        changePrice();
    })

    function changePrice(){
        var pricing_type = $("input[name='pricing_type']:checked").val();
        $('.sale_price').each(function(){
            var price = parseFloat($(this).val());
            var proxy_id = $(this).data('proxy_id');
            var pid = $(this).data('pid');
            var supply_pid = $(this).data('supply_pid');
            var sku_id = $(this).data('sku_id');
            $('#gross_'+proxy_id+'_'+sku_id).removeClass('red');

            if (pricing_type == 1) {
                price = shop_price / 100 * price;
            }

            $('.superior_price_'+proxy_id+'_'+sku_id).html(price.toFixed(2)+'元');
            $('.superior_price_'+proxy_id+'_'+sku_id).data('price',price);

            if (proxy_id < 1 || price <= 0){
                return true;
            }

            var gross_price = price - $('.superior_price_'+supply_pid+'_'+sku_id).data('price');

            if (gross_price < 0){
                $('#gross_'+proxy_id+'_'+sku_id).addClass('red');
            }
            $('#gross_'+proxy_id+'_'+sku_id).html(gross_price.toFixed(2)+'元');
            var gross_pre = gross_price / price  * 100;
            $('#gross_pre_'+proxy_id+'_'+sku_id).html(gross_pre.toFixed(2)+'%');
        })
    }

    function addConvertUnit(){
        var select_unit = $('#select_unit').val();
        if (select_unit < 1){
            return _alert('请先选择默认计量单位.',true);
        }
        var select_convert_unit = $('#select_convert_unit').val();
        if (select_convert_unit < 1){
            return _alert('请选择换算单位.',true);
        }
        if (select_unit == select_convert_unit){
            return _alert('换算单位不能选择默认计量单位.',true);
        }
        var isrep = false;
        $('#convert_unit_box').find('input[name="convert_unit_id[]"]').each(function(){
            if (select_convert_unit == $(this).val()) return isrep = true;
        })
        if (isrep == true) return _alert('列表中已存在此换算单位.',true);
        var select_unit_text = $('#select_unit').find("option:selected").text();
        var select_convert_unit_text = $('#select_convert_unit').find("option:selected").text();
        $('#convert_unit_box').append('<label><input type="hidden" name="convert_unit_id[]" value="'+select_convert_unit+'" >1'+select_convert_unit_text+' = <input class="input-ssmall m-t" data-rule-required="ture" data-rule-number="true" name="convert_unit_val[]" type="text"> '+select_unit_text+' <a href="javascript:;" class="text-muted del_convert_unit"><i class="fa fa-times"></i></a></label>');
    }
    $(document).on('click','.del_convert_unit',function () {
        $(this).parent().remove();
    })
    $(document).on('click','.pricing_type',function () {
        setPriceBox();
        $('.sale_price').val(0);
        $('.gross').html('--');
        changePrice();

    })
</script>
{/block}